/**
 * Dashboard.Share
 * Must handle everything related to sharing
 *
 * @author Seth
 * @version 0.3
 */
Dashboard.Share = function() {
	var open = false,
		inited = false,
		store = {
			entry: 0,
			perm: {
				d: 0,
				u: false
			}
		},

	// entry post
	_share_post = function() {
		var a = $('div.post_entry');

		// only load if on dashboard
		if(Dashboard.store().location == 'dashboard' && a.length > 0)
			Dashboard.Main.load(a);

		// send the post
		Petolio.ajax({
			url: 'dashboard/share',
			type: 'post',
			data: {v: store.entry, p: store.perm},
			cache: false,
			success: function () {
				// only unload and reload if on dashboard
				if(Dashboard.store().location == 'dashboard' && a.length > 0) {
					Dashboard.Main.unload(a);
					Dashboard.Main.reload();
				} else Petolio.showMessage(_translate('share_confirm'));
			}
		});

	// the link dialog
	}, _share_dialog = function() {
    	var identity = 'dialog-link';

    	// create the dialog
    	$("#"+ identity).remove();
		$('<div id="'+ identity +'" title="'+ _translate('share_title') +'" class="ui-state-highlight">'+
				'<div style="margin: 8px 0px 2px 0px; line-height: 18px;">'+
					_translate('share_txt')+
				'</div>'+
			'</div>').dialog({
				resizable: false,
				modal: true,
    			width: 300,
    			minHeight: 50,
				buttons: [{
					text: _translate('share'),
					click: function() {
						$(this).dialog("close");
						_share_post();
					}
				},{
					text: _translate('close'),
					click: function() {
						$(this).dialog("close");
					}
				}],
				close: function() {
					$("#"+ identity).remove();
				},
				open: function() {
					// append our privacy button
					_privacy_append($(this));
					$("#"+ identity).next().find('button').blur();
				}
			});

	}, _privacy_append = function(c) {
		var b = c.next('div.ui-dialog-buttonpane'),
			a = c.closest('div.ui-dialog');

		a.css('overflow', 'visible');
		b.append('<div class="privacy_share">'+
			'<span class="privacy_wrap" style="z-index: 1005;">'+
				'<i class="'+'p_' + Dashboard.Privacy.perms(store.perm.d)+'"></i>'+
				'<i class="p_down"></i>'+
				'<ul>'+
					'<dfn></dfn>'+
					'<li '+(store.perm.d == 0 ? 'class="active"' : '')+'><i class="p_public margin"></i><a href="javascript:void(0);">'+ _translate('public') +'</a></li>'+
					'<li '+(store.perm.d == 1 ? 'class="active"' : '')+'><i class="p_friends margin"></i><a href="javascript:void(0);">'+ _translate('friends') +'</a></li>'+
					'<li '+(store.perm.d == 2 ? 'class="active"' : '')+'><i class="p_me margin"></i><a href="javascript:void(0);">'+ _translate('only_me') +'</a></li>'+
					'<li '+(store.perm.d == 3 ? 'class="active"' : '')+'><i class="p_custom margin"></i><a href="javascript:void(0);">'+ _translate('custom') +'</a></li>'+
				'</ul>'+
			'</span>'+
		'</div>');

		// behave as buttons
		$('div.privacy_share').disableSelection();

	// open privacy dropdown
	}, _open = function() {
		var x = $(this);

		// close other privacy dialogs
		$('div.entry').find('span.privacy_wrap').each(function(k, v) {
			Dashboard.Privacy.close($(v));
		});

		// already opened somewhere? close that.
		if(open) return _close();

		// open
		x.addClass('privacy_down');
		x.find('ul').show();
		$(x.find('i').get(1)).removeClass().addClass('p_up');

		// set as opened
		open = true;
		return false;

	// close privacy dropdown
	}, _close = function() {
		var x = $("div.privacy_share > span.privacy_wrap");

		x.removeClass('privacy_down');
		x.find('ul').hide();
		$(x.find('i').get(1)).removeClass().addClass('p_down');

		open = false;

	// save privacy permissions
	}, _save_privacy = function() {
		var x = $(this),
			v = x.find('i').attr('class').replace(' margin', ''),
			b = x.closest('div.entry').data('id');

		// load custom dialog for custom state
		if(v == 'p_custom') {
			Dashboard.Privacy.Custom.init(x, b, Dashboard.Privacy.findPerm(v), v, Dashboard.Share);
			return true;
		}

		// skip save if we click the same state again
		if(x.hasClass('active'))
			return true;

		// save
		store.perm = {d: Dashboard.Privacy.findPerm(v), u: false};
		Dashboard.Privacy.changeState(x, v);

	// save perm from custom
	}, _save_perm = function(x) {
		store.perm = x;

	// bind
	}, _bind = function() {
		// privacy events
		$("html").delegate('div.privacy_share > span.privacy_wrap', 'click', _open);
		$("html").delegate('div.privacy_share > span.privacy_wrap > ul > li', 'click', _save_privacy);
		$("html").delegate('div', 'click', function() {
			// close privacy
			_close();
		});

    // translate
	}, _translate = function(i) {
		return Dashboard.Translate.init(i);

	// constructor
	}, __construct = function(e) {
		store.entry = e;

		if(!inited)
			_bind();

		inited = true;
		_share_dialog();
	};

	// public
	return {
		init: __construct,
		savePerm: _save_perm,
		store: function(){
			return store;
		}
	};
}();